//
//  NetworkViewController.swift
//  WoodpeckerMacDemo
//
//  Created by xiaogang zhang on 2019/6/1.
//  Copyright © 2019 LIFE BETTER. All rights reserved.
//

import Cocoa

class NetworkViewController: NSViewController,NSURLConnectionDataDelegate,URLSessionDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func requestButtonClicked(_ sender: Any) {
        //url connection
        let requestURL = URL.init(string: "https://api.github.com")
        let request = URLRequest.init(url: requestURL!)
        let connection = NSURLConnection.init(request: request, delegate: self)
        connection?.start()
        //session request
        self.sessionRequest()
    }
    
    func sessionRequest(){
        let config  = URLSessionConfiguration.default
        config.timeoutIntervalForRequest = 10.0
        let mySession = URLSession.init(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
        let url = "https://api.github.com/emojis"
        let requestURL = URL.init(string: url)
        let uploadRequest = NSMutableURLRequest.init(url: requestURL!)
        uploadRequest.httpMethod = "POST"
        let formText = "q=\(arc4random())"
        let data = formText.data(using: String.Encoding.utf8)
        let task = mySession.uploadTask(with: uploadRequest as URLRequest, from: data) { (data:Data?, response:URLResponse?, error:Error?) in
        }
        task.resume()
    }
    
    func connectionDidFinishLoading(_ connection: NSURLConnection) {
        
    }
    
}
